<?php
/**
 * 视图
 * 视图基类,用于定义模板的绘制和头部输出
 * @author LeoNing 2010-11-14 12:25
 *
 */
abstract class View {
	protected $tpl = '';
	public function __construct($tpl = '') {
		$this->setTpl ( $tpl );
	}
	/**
	 * 绘制
	 * @param array $data
	 * @param Response $response 
	 * @param string $tpl 模板
	 * @return string	 
	 */
	public abstract function render($data, $response, $tpl = '');
	/**
	 * 
	 * 设置输出头
	 */
	public function setHeader() {
	}
	/**
	 * 设置模板
	 * @param string $tpl
	 */
	public function setTpl($tpl) {
		if ($tpl) {
			$this->tpl = str_replace ( '/', DS, $tpl );
		}
	}
	/**
	 * 取得模板
	 * @return string 模板
	 */
	public function getTpl() {
		return empty ( $this->tpl ) ? null : $this->tpl;
	}
}
class Jquery {
	private $js = array ();
	/**
	 * 
	 * jquery选择符
	 * @param string $selector
	 */
	public function __construct($selector) {
		$this->js [] = "$('{$selector}')";
	}
	/**
	 * 
	 * 添加操作
	 * @return PQ
	 */
	public function live() {
		$args = func_get_args ();
		if (! empty ( $args )) {
			$action = array_shift ( $args );
			$live = array ();
			foreach ( $args as $arg ) {
				$live [] = is_string ( $arg ) ? "'" . str_replace ( "'", "\'", $arg ) . "'" : (is_array ( $arg ) ? json_encode ( $arg ) : $arg);
			}
			$this->js [] = $action . '(' . implode ( ',', $live ) . ')';
		}
		return $this;
	}
	public function toJS() {
		if (count ( $this->js ) >= 2) {
			return implode ( '.', $this->js ) . ';';
		}
		return '';
	}
}
/**
 * 页面头可以使用的插件
 * 
 * @param boolean $cmd 是否cmd的do_page_header
 */
function do_page_header() {
	global $__cmd, $APPLICATION, $ACTION_NAME, $__page_res;
	fire ( 'do_page_header', $APPLICATION, $ACTION_NAME );
	fire ( 'do_page_header_' . $APPLICATION, $ACTION_NAME );
	if ($__cmd != null && is_subclass_of ( $__cmd, 'Action' )) {
		$__cmd->do_page_header ();
	}
	if (! empty ( $__page_res )) {
		if (isset ( $__page_res ['css'] ['x'] )) {
			ksort ( $__page_res ['css'] ['x'] );
			foreach ( $__page_res ['css'] ['x'] as $csses ) {
				echo implode ( "\n", $csses ) . "\n";
			}
		}
		if (isset ( $__page_res ['style'] ) && ! empty ( $__page_res ['style'] )) { //头部STYLE
			echo "<style type=\"text/css\">";
			foreach ( $__page_res ['style'] as $s => $style ) {
				echo $s, $style;
			}
			echo "</style>\n";
		}
		if (isset ( $__page_res ['p2j'] )) { //js变量设置
			ejvars ();
		}
		if (isset ( $__page_res ['js'] ['h'] )) { //头部JS
			ksort ( $__page_res ['js'] ['h'] );
			foreach ( $__page_res ['js'] ['h'] as $jses ) {
				echo implode ( "\n", $jses ) . "\n";
			}
		}
	}
}
/**
 * 页面底可以使用的插件
 * 
 * @param boolean $cmd 是否cmd的do_page_footer
 */
function do_page_footer() {
	global $__cmd, $APPLICATION, $ACTION_NAME, $__page_res;
	fire ( 'do_page_footer', $APPLICATION, $ACTION_NAME );
	fire ( 'do_page_footer_' . $APPLICATION, $ACTION_NAME );
	if ($__cmd != null && is_subclass_of ( $__cmd, 'Action' )) {
		$__cmd->do_page_footer ();
	}
	if (isset ( $__page_res ['js'] ['f'] )) {
		ksort ( $__page_res ['js'] ['f'] );
		foreach ( $__page_res ['js'] ['f'] as $jses ) {
			echo implode ( "\n", $jses ) . "\n";
		}
	}
	ejquery ();
}
/**
 * 
 * 生成页面标题,给插件修改标题的机会
 * @param string $title 原始页面标题
 */
function page_title($title) {
	return apply_filter ( 'get_page_title', $title );
}

/**
 * 
 * 将js加入到页面js输出队列
 * 
 * @param string $js js文件
 * @param int $order 顺序
 * @param boolean $header 是否在头部输出
 */
function pagejs($js, $order = 10, $header = true) {
	global $__page_res;
	$pos = $header ? 'h' : 'f';
	$mkey = md5 ( $js );
	if (! isset ( $__page_res ['js'] [$mkey] )) {
		$__page_res ['js'] [$pos] [$order] [] = '<script type="text/javascript" src="' . $js . '"></script>';
		$__page_res ['js'] [$mkey] = true;
	}
}
/**
 * 
 * 将CSS加入到页面CSS输出队列
 * @param string $css
 * @param int $order
 */
function pagecss($css, $order = 10) {
	global $__page_res;
	$mkey = md5 ( $css );
	if (! isset ( $__page_res ['css'] ['x'] [$mkey] )) {
		$__page_res ['css'] ['x'] [$order] [] = '<link href="' . $css . '" type="text/css" rel="stylesheet" />';
		$__page_res ['css'] [$mkey] = true;
	}
}
/**
 * 
 * 将PHP 变量转换为JS变量,如果PHP变量为ARRAY,则转换为json.
 * @param string $name
 * @param mixed $val
 */
function p2jvar($name, $val = null) {
	global $__page_res;
	if (is_array ( $name )) {
		foreach ( $name as $n => $val ) {
			$__page_res ['p2j'] [$n] = $val;
		}
	} else {
		$__page_res ['p2j'] [$name] = $val;
	}
}
/**
 * 设置直接输出到页面的样式
 * @param mixed $selector 选择符或选择符样式数组
 * @param string $style 样式
 */
function style($selector, $style = '') {
	global $__page_res;
	if (is_array ( $selector )) {
		foreach ( $selector as $s => $style ) {
			$__page_res ['style'] [$s] = "{{$style}} ";
		}
	} else {
		$__page_res ['style'] [$selector] = "{{$style}} ";
	}
}
/**
 * 
 * 输出js变量到html页面,在输出之前要调用 p2jvar 设置才行
 * @param mixed $names 要输出的变量,为空时,输出全部
 */
function ejvars($names = '') {
	global $__page_res;
	
	if (empty ( $names ) && ! isset ( $__page_res ['p2j'] )) {
		return;
	} else if (empty ( $names )) {
		$names = array_keys ( $__page_res ['p2j'] );
	}
	settype ( $names, 'array' );
	if (isset ( $__page_res ['p2j'] )) {
		echo '<script type="text/javascript">';
		foreach ( $names as $name ) {
			if (! isset ( $__page_res ['p2j'] [$name] )) {
				continue;
			}
			$var = $__page_res ['p2j'] [$name];
			echo 'var ', $name, '=';
			if (is_string ( $var )) {
				echo "'", str_replace ( "'", "\\'", $var ), "'";
			} else if (is_array ( $var )) {
				echo json_encode ( $var );
			} else if (is_numeric ( $var ) || is_bool ( $var )) {
				echo $var;
			} else {
				echo "null";
			}
			echo ";";
		}
		echo "</script>\n";
	}
}
/**
 * 
 * 动态生成JQUERY代码
 * @param Jquery $query
 */
function jquery($jquery) {
	global $__page_res;
	if ($jquery instanceof Jquery) {
		$__page_res ['jquery'] [] = $jquery;
	}
}
/**
 * 
 * 输出JQUERY代码
 */
function ejquery() {
	global $__page_res;
	if (isset ( $__page_res ['jquery'] )) {
		echo '<script type="text/javascript">$(function(){';
		foreach ( $__page_res ['jquery'] as $pq ) {
			echo $pq->toJS ();
		}
		echo "});</script>\n";
	}
}
/**
 * 
 * 设置表单元素值
 * @param array $vars
 */
function setformvars($vars) {
	foreach ( $vars as $name => $value ) {
		$pq = new Jquery ( '#' . $name );
		$pq->live ( 'val', $value );
		jquery ( $pq );
	}
}

function treetpl($data, $treekey, $subkey = 'items', $replace = array(), $header = '', $footer = '', $tab = 0) {
	if (empty ( $data )) {
		return;
	}
	settype ( $data, 'array' );
	foreach ( $data as $d ) {
		if (isset ( $d [$treekey] )) {
			$tpl = $header . trees ( $d [$treekey], $tab ) . $footer;
			foreach ( $replace as $r ) {
				if (isset ( $d [$r] )) {
					$tpl = str_replace ( '{' . $r . '}', $d [$r], $tpl );
				}
			}
			echo preg_replace ( '/([\d]+)=\\1/', 'on', $tpl );
			if (isset ( $d [$subkey] ) && ! empty ( $d [$subkey] )) {
				treetpl ( $d [$subkey], $treekey, $subkey, $replace, $header, $footer, $tab + 1 );
			}
		}
	}
}